package com.yk.faceapplication.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.allinpay.xed.R;
import com.android.volley.VolleyError;
import com.yk.faceapplication.callback.LoanPurposeCallback;
import com.yk.faceapplication.callback.OptionCallback;
import com.yk.faceapplication.callback.RequestCallback;
import com.yk.faceapplication.entity.Purpose;
import com.yk.faceapplication.entity.StudyInfo;
import com.yk.faceapplication.http.RequestUrl;
import com.yk.faceapplication.util.ActivityUtil;
import com.yk.faceapplication.util.RequestUtil;

import org.json.JSONException;
import org.json.JSONObject;

public class MarketActivity extends BaseActivity {
    private Button commitBtn;
    private RelativeLayout loanTermLayout,loanPurposeLayout;
    private Dialog loanTermDialog;
    private TextView loanTermTV,loanPurposeTV;
    private String loanTermCode,loanPurposeCode;
    private Dialog loanPurposeDialog;
    private EditText loanAmountET;

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (R.id.commit_btn == v.getId()) {
            Intent intent = new Intent(MarketActivity.this, MarketListActivity.class);
            if(!ActivityUtil.editIsNull(loanAmountET)) intent.putExtra("minLoanAmt",loanAmountET.getText().toString());
            if(loanPurposeCode != null) intent.putExtra("loanUse",loanPurposeCode);
            if(loanTermCode != null) intent.putExtra("extend1",loanTermCode);
            startActivity(intent);
        }else if(R.id.loan_term_layout == v.getId()){
            loanTermDialog.show();

        }else if(R.id.loan_purpose_layout == v.getId()){
            loanPurposeDialog.show();
        }
    }




    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setTitle("借款超市");
        commitBtn = (Button) findViewById(R.id.commit_btn);
        commitBtn.setOnClickListener(this);
        loanTermLayout = (RelativeLayout) findViewById(R.id.loan_term_layout);
        loanTermLayout.setOnClickListener(this);
        loanPurposeLayout = (RelativeLayout) findViewById(R.id.loan_purpose_layout);
        loanPurposeLayout.setOnClickListener(this);
        loanTermTV = (TextView) findViewById(R.id.loan_term_tv);
        loanPurposeTV = (TextView) findViewById(R.id.loan_purpose_tv);
        loanAmountET = (EditText) findViewById(R.id.loan_amount_et);

        //在一开始声明TextWatcher，在afterTextChange内操作
        TextWatcher mTextWatcher=new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                String text = s.toString();
                int len = s.toString().length();
                if (len == 1 && text.equals("0")) {
                    s.clear();
                }
            }
        };

        loanAmountET.addTextChangedListener(mTextWatcher);

//        loanAmountET.addTextChangedListener(new TextWatcher() {
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//                // 输入的内容变化的监听
//            }
//
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                // 输入前的监听
//
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                // 输入后的监听
//                checkInputInfo();
//            }
//        });

        checkInputInfo();

        getPurposes();
        getLoanTerm();

    }

    private void checkInputInfo(){
//        if(!ActivityUtil.editIsNull(loanAmountET) || !ActivityUtil.textIsNull(loanTermTV) || !ActivityUtil.textIsNull(loanPurposeTV)){
//            commitBtn.setBackgroundColor(getResources().getColor(R.color.textRegisterFile));
//            commitBtn.setEnabled(true);
//        }else{
//            commitBtn.setBackgroundColor(getResources().getColor(R.color.bg_gray));
//            commitBtn.setEnabled(false);
//        }
        if(mApplication.getLoanTerms() != null && mApplication.getPurposes() != null){
                        commitBtn.setBackgroundColor(getResources().getColor(R.color.textRegisterFile));
                        commitBtn.setEnabled(true);
        }
    }

    private void getPurposes() {

        RequestCallback callback = new RequestCallback() {
            @Override
            public void success(JSONObject jsonObject) {
                LoanPurposeCallback callback = new LoanPurposeCallback() {
                    @Override
                    public void success(Purpose option) {
                        loanPurposeTV.setText(option.getValue());
                        loanPurposeDialog.dismiss();
                        loanPurposeCode = option.getCode();

                    }

                    @Override
                    public void cancel() {
                        loanPurposeDialog.dismiss();
                    }
                };
                try {
                    mApplication.setPurposes(JSON.parseArray(jsonObject.getString("data"), Purpose.class));
                    loanPurposeDialog = ActivityUtil.createLoanPurposeDialog(MarketActivity.this, mApplication.getPurposes(), callback);
                    checkInputInfo();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail(VolleyError volleyError) {

            }
        };
        RequestUtil.getRequest(this, RequestUrl.GET_PURPOSE + "token=" + mToken, callback);
    }

    private void getLoanTerm() {

        RequestCallback callback = new RequestCallback() {
            @Override
            public void success(JSONObject jsonObject) {

                try {
                    OptionCallback callback = new OptionCallback() {
                        @Override
                        public void success(StudyInfo option) {
                            loanTermTV.setText(option.getValue());
                            loanTermDialog.dismiss();
                            loanTermCode = option.getCode();

                        }

                        @Override
                        public void cancel() {
                            loanTermDialog.dismiss();
                        }
                    };
                    mApplication.setLoanTerms(JSON.parseArray(jsonObject.getString("data"), StudyInfo.class));
                    loanTermDialog = ActivityUtil.createLoanTermDialog(MarketActivity.this, mApplication.getLoanTerms(), callback);
                    checkInputInfo();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail(VolleyError volleyError) {

            }
        };
        RequestUtil.getRequest(this, RequestUrl.GET_LOAN_TERM + "token=" + mToken, callback);
    }
}
